package fi.laurea.camera;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.google.gson.Gson;

import fi.laurea.examples.base64.BufferImageBase64Coder;
import fi.laurea.jsonbus.Receiver;
import fi.laurea.jsonbus.ReceiverListener;

public class CameraUI2 extends JPanel implements ReceiverListener {
	
	Receiver receiver;
	private JTextArea area;
	private JTextArea textArea2;
	private Gson gson;
	private ImageIcon image;
	private JLabel label;
	
	public CameraUI2() {
		
		receiver = new Receiver("camera", "ilkka", this);
		this.area = new JTextArea();
		this.gson = new Gson();
		this.image = new ImageIcon();
		this.label = new JLabel(image);
		this.textArea2 = new JTextArea();
		JTextArea textArea3 = new JTextArea();
		JTextArea textArea4 = new JTextArea();
		JTextArea textArea5 = new JTextArea();
		JTextField field = new JTextField();
		JFrame frame = new JFrame("Web Camera");
		JScrollPane scroll = new JScrollPane(textArea5);
		
		JButton button = new JButton("<");
		JButton button2 = new JButton(">");
		JButton button3 = new JButton("DELETE ALL");
		
		frame.setLayout(new BorderLayout());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		frame.add(area);
		frame.add(textArea2);
		frame.add(textArea3);
		frame.add(textArea4);
		frame.add(textArea5);
		textArea5.add(scroll, BorderLayout.LINE_START);
		frame.add(label);
		frame.add(this);
		label.setBackground(Color.red);
		frame.add(field, BorderLayout.SOUTH);
		frame.add(button);
		frame.add(button2);
		frame.add(button3);
		
		area.setSize(80, 30);
		area.setLocation(10, 20);
		textArea2.setSize(80, 30);
		textArea2.setLocation(105, 20);
		textArea3.setSize(80, 30);
		textArea3.setLocation(10, 60);
		textArea4.setSize(80, 30);
		textArea4.setLocation(105, 60);
		textArea5.setSize(200, 200);
		textArea5.setLocation(10, 100);
		this.setLocation(600, 30);
		label.setSize(350, 350);
		label.setLocation(300, 200);
		frame.getContentPane().add(this);
		button.setSize(60, 60);
		button.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
			
			if (e.getButton() == MouseEvent.BUTTON1) {
			
			textArea2.setText("Button 1 clicked...");
			
			}
	
			}
			
			});
			
			 
		button2.setSize(60, 60);
		button2.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
			
			if (e.getButton() == MouseEvent.BUTTON1) {
			
			textArea2.setText("Button 2 clicked...");
			
			}
	
			}
			
			});
		button3.setSize(150, 150);
		button3.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
			
			if (e.getButton() == MouseEvent.BUTTON1) {
			
			textArea2.setText("Button 3 clicked...");
			
			}
	
			}
			
			});
		button3.setBackground(Color.red);
		button.setLocation(240, 455);
		button2.setLocation(480, 455);
		button3.setLocation(50, 500);

		frame.setSize(800, 800);
		

		
		frame.setVisible(true);
	}


	public JTextArea getArea() {
		return area;
	}


	public void setArea(JTextArea area) {
		this.area = area;
	}


	public Gson getGson() {
		return gson;
	}


	public void setGson(Gson gson) {
		this.gson = gson;
	}


	public ImageIcon getImage() {
		return image;
	}


	public void setImage(ImageIcon image) {
		this.image = image;
	}


	public JLabel getLabel() {
		return label;
	}


	public void setLabel(JLabel label) {
		this.label = label;
	}
	
	public void prosessGloballMessage(String json) {
		CameraMessage message = (CameraMessage) gson.fromJson(json,
				CameraMessage.class);
		
		BufferedImage img = null;
		try {
			img = BufferImageBase64Coder.decode(message.getImage ());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		image.setImage(img);		
		this.area.setText(message.getDate());
		label.setSize(350, 350);
		label.setLocation(300, 200);
		area.setSize(80, 30);
		area.setLocation(10, 20);
		this.add(label);
		this.revalidate();
		this.repaint();
		
		
	
		
		
	}

	@Override
	public void prosessPrivateMessage(String json) {
		CameraMessage message = (CameraMessage) gson.fromJson(json,
				CameraMessage.class);
		
		BufferedImage img = null;
		try {
			img = BufferImageBase64Coder.decode(message.getImage ());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		image.setImage(img);
		this.add(label);
		//this.add(new JLabel(new ImageIcon(img)));
		//this.area.setText(message.getDate());
	}

	public static void main(String args[]) {
		
		
		SwingUtilities.invokeLater(new Runnable() {
			
			public void run() {
			
				new CameraUI2();
			}
	


	

});
	}}